template.defaults.imports.splitImages = function (imgsStr) {
    var imgs = imgsStr.split(',');
    template.defaults.imports.imgs = imgs;
};

template.defaults.imports.getVideoInfo = function (videoId) {
    $.ajaxSettings.async = false;
    $.get('/listSnapshots',{
        videoId:videoId,
    },function(result){
        if (result.code == 100){
            var url = result.data.MediaSnapshot.Snapshots.Snapshot[0].Url;
            $.get('/getPlayInfo?videoId=' + videoId,function(result){
                var playVideo = '';
                for (var i = 0;i < result.data.length; i++){
                    if (result.data[i].Format == 'mp4'){
                        playVideo = result.data[i].PlayURL;
                        break;
                    }
                }
                template.defaults.imports.video = {
                    playUrl:playVideo,
                    imgUrl:url
                };
                $.ajaxSettings.async = true;
            });
        }
    });
};

template.defaults.imports.CountdownTime = function (factEndDate) {
    var timestamp = (new Date()).valueOf();
    return factEndDate - timestamp;
};

template.defaults.imports.TimestampToTime = function (timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes();
    return Y + M + D + h + m ;
};

template.defaults.imports.ellipsisName = function (name) {
    var strLen = name.length;
    if (strLen > 2){
        name = name.substr(1); //删除第一个字符
        name = name.substr(0, name.length - 1);
        return '*' + name + '*';
    } else if (strLen > 1){
        return name.substr(0, 1) + '*';
    } else {
        return name;
    }
};

template.defaults.imports.sendTime = function (createDate) {
    var timestamp = (new Date()).valueOf();
    var diffiTime = timestamp - createDate;
    //计算天
    var day = Math.floor(diffiTime / (1000 * 60 * 60 * 24));
    var surplus = diffiTime % (1000 * 60 * 60 * 24);
    var hour = Math.floor(surplus / (1000 * 60 * 60));
    surplus = surplus % (1000 * 60 * 60);
    var minute = Math.floor(surplus / (1000 * 60));
    return (day ? day + '天' : '') + (hour ? hour + '小时' : '') + (minute ? minute + '分' : '') + '前';
};

//查询商品详情
var productDetail = {};
$.post('/antique/api/mallConsignOrder/query', {
    /* global getCookie visitUrl*/
    token: getCookie('token'),
    /* global id */
    id: id
}, function (result) {
    /* global template */
    var html = template('detail', result);
    document.getElementById('detail-content').innerHTML = html;
    productDetail = result.data;

    $(".swiper-container").swiper({
        loop: true
    });

    var images = result.data.showImages.split(',');
    $(".swiper-slide").click(function() {
        var index = $(this).data('swiper-slide-index');
        var playerContainer = $('<div></div>').attr('id','playerContainer').css({
            position:'fixed',
            width:'100%',
            height:'100%',
            display:'none',
            top: '0',
            zIndex:'9999'
        }).appendTo(window.document.body);
        if ($('.swiper-slide[data-swiper-slide-index="' + index + '"]').hasClass('video')){
            var playUrl = $('.swiper-slide[data-swiper-slide-index="' + index + '"]').attr('data-url');
            var player = new Aliplayer({
                "id": "playerContainer",
                "source": playUrl,
                "width": "100%",
                "height": "500px",
                "autoplay": true,
                "isLive": false,
                "rePlay": true,
                "playsinline": true,
                "preload": true,
                "controlBarVisibility": "hover",
                "useH5Prism": true
            }, function (player) {
                player._switchLevel = 0;
                console.log("播放器创建了。");
            });
            var closeVideo = $('<svg id="closeVideo" t="1552392959791" class="icon" style="width:0.8rem;height:0.8rem;position:absolute;top:0.1rem;right:0.1rem;cursor:pointer;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5486" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M512 32C246.4 32 32 246.4 32 512s214.4 480 480 480 480-214.4 480-480S777.6 32 512 32" fill="#FF2828" p-id="5487"></path><path d="M713.6 713.6c-12.8 12.8-35.2 12.8-48 0l-153.6-153.6-153.6 153.6c-12.8 12.8-35.2 12.8-48 0s-12.8-35.2 0-48l153.6-153.6-153.6-153.6c-12.8-12.8-12.8-35.2 0-48s35.2-12.8 48 0l153.6 153.6 153.6-153.6c12.8-12.8 35.2-12.8 48 0s12.8 35.2 0 48l-153.6 153.6 153.6 153.6c16 12.8 16 35.2 0 48z" fill="#FFFFFF" p-id="5488"></path></svg>');
            playerContainer.append(closeVideo).show();
            closeVideo.click(function(){
                var _parent = $(this).parent();
                player.stop();
                _parent.remove();
            });
        } else {
            var currentUrl = $('.swiper-slide[data-swiper-slide-index="' + index + '"] img').attr('data-url');
            for (var i = 0;i < images.length;i++){
                images[i] = images[i];
            }
            wx.previewImage({
                current: currentUrl, // 当前显示图片的http链接
                urls: images // 需要预览的图片http链接列表
            });
        }        
    });

    $('#standard_offer_target').click(function(){
        $('#sendPrice').val('');
        $("#standard_offer").popup();
    });   

    

   });

   
   
   
   
    $('textarea[name="report-content"]').keyup(function(e){
        var text = $(this).val();
        $('.weui-textarea-counter span').text(text.length);
        if(text.length < 20){
            $('.weui-textarea-counter span').removeClass('green').addClass('red');
        } else if(text.length > 200){
            $('.weui-textarea-counter span').removeClass('green').addClass('red');
        } else {
            $('.weui-textarea-counter span').removeClass('red').addClass('green');
        }
    });


$('.cdetail-bottom .cdetail-icon-item').click(function(){
    $('.cdetail-bottom .cdetail-icon-item').removeClass('current');
    $(this).addClass('current');
});




